package com.cy.controller;

import com.cy.pojo.Merch;
import com.cy.service.MerchService;
import com.cy.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 前台展现
 * @author qyq
 * @create 2021-03-01-14:33
 */
@Controller
public class MerchController {
    @Autowired
    private MerchService merchService;


    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/merch-add")
    public String addIndex() {
        return "index-add";
    }


    /*
        后台查询所有商品，前台展现json
     */
    @RequestMapping("merch/findAll")
    @ResponseBody
    public JsonResult findAll(){
        return new JsonResult().success(merchService.findMerchs());
    }

    @RequestMapping("doDeleteId/{id}")
    public String doDeleteId(@PathVariable Integer id){
        merchService.deleteId(id);
        return "redirect:/index";
    }

    @RequestMapping("doFindId/{id}")
    public String doFindId(@PathVariable Integer id, Model model) {
        Merch merch=merchService.findId(id);
        model.addAttribute("merch",merch);
        return "merch-update";
    }
    @RequestMapping("doUpdateMerch")
    public String doUpdateMerch(Merch merch) {
        merchService.updateMerch(merch);
        return "redirect:/index";
    }
}
